package dtd.PHS.VND_ExchangeRate.ui;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import dtd.PHS.VND_ExchangeRate.R;
import dtd.PHS.VND_ExchangeRate.UpdateRatesService;
import dtd.PHS.VND_ExchangeRate.VNE_Preferences;
import dtd.PHS.VND_ExchangeRate.data.UpdateGoldService;
import dtd.PHS.VND_ExchangeRate.utils.Helpers;

public class MyMainMenu {
	private Activity activity;
	public MyMainMenu(Activity activity) {
		this.activity = activity;
	}
	public void createOptionsMenu(Menu menu) {
		MenuInflater inflater = activity.getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);		
	}
	public boolean onItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_refresh:
			refreshRates();
			return true;
		case R.id.menu_language_vi:
			Helpers.saveLanguageOption(Helpers.VIETNAMESE);
			Helpers.changeLanguage(activity,Helpers.VIETNAMESE);
			Helpers.forceActivityRestart(activity);
			return true;
		case R.id.menu_language_en:
			Helpers.saveLanguageOption(Helpers.ENGLISH);
			Helpers.changeLanguage(activity,Helpers.ENGLISH);
			Helpers.forceActivityRestart(activity);
			return true;
		case R.id.menu_preferences:
			Intent i = new Intent( activity, VNE_Preferences.class);
			activity.startActivity(i);
			return true;
		case R.id.menu_exit:
			Helpers.launchHomeScreen(activity);
			return true;
		default: return false;
		}
	}
	private void refreshRates() {
		Intent i = new Intent(activity,UpdateRatesService.class);
		activity.startService(i);
		i = new Intent(activity,UpdateGoldService.class);
		activity.startService(i);
	}

}
